﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using ReflectionStudio.Core.Events;
using System.Windows;
namespace WPFExcelReport
{

    public class YarnMasterData : BaseFunction
    {


        public YarnMasterData()
            : base()
        {

        }
        public DataSet LoadTableStructure(DataSet dsTarget)
        {
            try
            {
                while (TableQueue.Count > 0)
                {
                    string TableName = TableQueue.Dequeue() as string;
                    orlReadCommand = OracleDB.CreateCommand();
                    orlReadCommand.CommandText = "SELECT * FROM INTEX." + TableName + " WHERE Rownum<1";
                    dsResult = Connect2Proxy(orlReadCommand);
                    if (dsResult.Tables.Count > 0)
                    {
                        dsResult.Tables[0].TableName = TableName;
                    }
                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        dsTarget = ImportDataTable(dsTarget, dtTemp);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public YarnMasterData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public YarnMasterData(OracleConnection _myConnection, IntexReptServBLClient _proxy)
            : base(_myConnection, _proxy)
        {

        }
        public bool YarnExist(DataTable dtGSDAT, string YarnArt, string YarnVar)
        {
            try
            {
                string Art = "GSART";
                string Var = "GSVAR";
                Art = "GSCART";
                Var = "GSCVAR";
                if (dtGSDAT.TableName == "GSDAT")
                {
                    Art = "GSART";
                    Var = "GSVAR";
                }
                if (dtGSDAT.TableName == "GASTGA")
                {
                    Art = "GAGART";
                    Var = "GAGVAR";
                }
                var query = from dwGSDAT in dtGSDAT.AsEnumerable()
                            where dwGSDAT.Field<string>(Art) == YarnArt
                        && dwGSDAT.Field<string>(Var) == YarnVar

                            select dwGSDAT;


                int Count = 0;
                foreach (var a in query)
                {
                    Count++;
                }
                if (Count == 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return true;
        }
        public DataSet GetYarnMaster(DataSet dsTarget, DataRow dwGSCOLOR)
        {
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT "
                + " GSART"
                + " ,GSVAR"
                + " ,GSMAT"
                + " ,GSNUMART"
                + " ,GSGARNNR"
                + " ,GSFACH"
                + " ,GSRECHNUM"
                + " ,GSRECHNR"
                + " ,GSGARNBEZ"
                 + " FROM INTEX.GSDAT WHERE GSART = :ArticleNum AND GSVAR=:ArticleVar";
                orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "GSDAT";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GSGARNNR");
                    Q100.Enqueue("GSRECHNR");
                    DivideBy100(dsResult.Tables[0], Q100);
                    Q100 = new Queue();
                    Q100.Enqueue("GSGARNNR");
                    Q100.Enqueue("GSRECHNR");
                    DivideBy100(dsResult.Tables[0], Q100);
                }
                if (dsResult.Tables[0].Rows.Count == 0)
                {

                    MessageBox.Show(dwGSCOLOR["GSCART"].ToString().Trim() + '/' + dwGSCOLOR["GSCVAR"].ToString() + "未找到！");
                }

                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet GetYarnColorWay(DataSet dsTarget, DataRow dwGSCOLOR)
        {
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT GSCART,GSCVAR,GSCFARBE,GSCBEZ FROM INTEX.GSCOLOR WHERE GSCART = :ArticleNum AND GSCVAR=:ArticleVar";
                orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "GSCOLOR";
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet GetTechnicalData(DataSet dsTarget, DataRow dwGSCOLOR)
        {
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT * FROM INTEX.BHSATZ WHERE BHKART = :ArticleNum AND BHKVAR=:ArticleVar";
                orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "BHSATZ";
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet GetYarnBOM(DataSet dsTarget, DataRow dwGSCOLOR)
        {
            try
            {

                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT "
                    + "GAGART"
                    + ",GAGVAR"
                    + ",GAGFARBE"
                    + ",GAGPOS"
                    + ",GAGGART"
                    + ",GAGGVAR"
                    + ",GAGGFARBE"
                    + ",GAGKGNET"
                    + ",GAGKGBRUT"
                   + " FROM INTEX.GASTGA WHERE GAGART = :ArticleNum AND GAGVAR=:ArticleVar";
                orlReadCommand.Parameters.Add("ArticleNum", OracleType.NVarChar).Value = dwGSCOLOR["GSCART"].ToString();
                orlReadCommand.Parameters.Add("ArticleVar", OracleType.NVarChar).Value = dwGSCOLOR["GSCVAR"].ToString();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = "GASTGA";
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public DataSet BuildYarnMasterData(DataSet dsTarget, DataRow dwGSCOLOR)
        {
            try
            {
                dsTarget = GetYarnMaster(dsTarget, dwGSCOLOR);
                dsTarget = GetYarnColorWay(dsTarget, dwGSCOLOR);
                string YarnArt = dwGSCOLOR["GSCART"].ToString();
                string YarnVar = dwGSCOLOR["GSCVAR"].ToString();

                if (!YarnExist(dsTarget.Tables["GASTGA"], YarnArt, YarnVar))
                {
                    dsTarget = GetYarnBOM(dsTarget, dwGSCOLOR);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
    }
}